package com.stylefeng.guns.modular.rmbCashReceipts.service.impl;

import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.modular.system.model.RmbCashReceipts;
import com.stylefeng.guns.modular.system.dao.RmbCashReceiptsMapper;
import com.stylefeng.guns.modular.rmbCashReceipts.service.IRmbCashReceiptsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-07-27
 */
@Service
public class RmbCashReceiptsServiceImpl extends ServiceImpl<RmbCashReceiptsMapper, RmbCashReceipts> implements IRmbCashReceiptsService {

    /**
     * 描述:下载
     * 创建人: laoyu
     * 日期: 2018/8/6 0006
     * 时间: 上午 9:39
     */
    @Override
    public Map<String, Object> load(List<RmbCashReceipts> rmbCashReceiptsList, String beginDate,String endDate, String platform) {
        Map<String,Object> resultMap = new HashMap<>();
        List<Map<String,Object>> loadMapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //组装下载数据
        double payoutTotal = 0;
        double incomeTotal = 0;
        for (RmbCashReceipts rmbCashReceipts : rmbCashReceiptsList){
            Map<String,Object> loadMap = new HashMap<>();
            loadMap.put("revDate",simpleDateFormat.format(rmbCashReceipts.getRevdate()));
            loadMap.put("summary",RmbCashReceipts.getDesc(rmbCashReceipts.getSummary()));
            loadMap.put("customer",rmbCashReceipts.getCustomer());
            if (rmbCashReceipts.getType()==RmbCashReceipts.Enum_Type.收款.value) {
                loadMap.put("collectionAmount", rmbCashReceipts.getAmount());
                incomeTotal += rmbCashReceipts.getAmount();
            }else {
                loadMap.put("payout", rmbCashReceipts.getAmount());
                payoutTotal += rmbCashReceipts.getAmount();
            }
            loadMapList.add(loadMap);
        }
        resultMap.put("rmbCashMap",loadMapList);
        resultMap.put("name",platform.toUpperCase()+beginDate+"--"+endDate+"RMB银行其它收入/其它支出明细分类表");
        resultMap.put("payoutTotal",payoutTotal);
        resultMap.put("incomeTotal",incomeTotal);

        return resultMap;
    }

    /**
     * 描述:删除报表后是修改所有报表余额
     * 创建人: laoyu
     * 日期: 2018/8/9 0009
     * 时间: 下午 5:18
     */
    @Override
    public void updateBalance(int id, String platform, double balance) throws GunsException {
            baseMapper.updateBalance(id,platform,-balance);
    }
}
